package com.ymatou.query.trading.api.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.trading.api.parameter.GetBuyedNumByProductBean;
import com.ymatou.query.trading.api.service.GetBuyedNumByProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.TradingQueryDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取已购买商品数量 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetBuyedNumByProduct {
	private static GetBuyedNumByProductBean gbnbpbean;
	private static GetBuyedNumByProductCall gbnbpcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取已购买商品数量");
	}

	@Before
	public void caseUp() {
		gbnbpbean = new GetBuyedNumByProductBean();
		gbnbpcall = new GetBuyedNumByProductCall();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gbnbpcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyedNumByProduct_001() {
		Logger.start(true,"获取已购买商品数量成功");
		try {
			int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			String date1 = YMTDateUtil.getBeforeOrNextDay(-365);
			String date2 = YMTDateUtil.getBeforeOrNextDay(1);
			Logger.comment("查询开始时间:" + date1);
			gbnbpbean.setBeginTime(date1);
			Logger.comment("买家Id:" + UserId);
			gbnbpbean.setBuyerId(UserId);
			Logger.comment("查询结束时间:" + date2);
			gbnbpbean.setEndTime(date2);
			// 订单状态
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(4);
			Logger.comment("订单状态:" + list);
			gbnbpbean.setOrderStatusList(list);
			// 产品ID
			String ProductId = "aa5ddccf-d911-4440-a4c7-d5d2259e4093";// sit数据
			Logger.comment("产品ID:" + ProductId);
			gbnbpbean.setProductId(ProductId);
			// 发送请求
			gbnbpcall.setData(gbnbpbean);
			gbnbpcall.callService();
			Logger.comment("gbnbpcall send:" + gbnbpcall.getEntityString());
			Logger.comment("gbnbpcall response:" + gbnbpcall.getReturnData());

			HashMap m = new HashMap();
			m.put("userId", UserId);
			m.put("tradingStatus", list);
			m.put("startDate", date1);
			m.put("endDate", date2);
			m.put("productId", ProductId);
			List<Map> data = new TradingQueryDefaultWapper().selectBuyedNumByProductid(m);
			Logger.verifyEquals(ECode.SUCCESS, gbnbpcall.getCode(), "验证Code");
			Logger.verifyEquals((data.get(0).get("ProductCount")), gbnbpcall.getProductCount(),
					"验证数据库查询与接口返回的数据是否一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyedNumByProduct_002() {
		Logger.start(true,"获取已购买商品数量成功查询所有时间");
		try {
			int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			String date1 = YMTDateUtil.getBeforeOrNextDay(-365);
			String date2 = YMTDateUtil.getBeforeOrNextDay(1);
			// Logger.comment("查询开始时间:"+date1);
			// gbnbpbean.setBeginTime(date1);
			Logger.comment("买家Id:" + UserId);
			gbnbpbean.setBuyerId(UserId);
			// Logger.comment("查询结束时间:"+date2);
			// gbnbpbean.setEndTime(date2);
			// 订单状态
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(4);
			Logger.comment("订单状态:" + list);
			gbnbpbean.setOrderStatusList(list);
			// 产品ID
			String ProductId = "aa5ddccf-d911-4440-a4c7-d5d2259e4093";// sit数据
			Logger.comment("产品ID:" + ProductId);
			gbnbpbean.setProductId(ProductId);
			// 发送请求
			gbnbpcall.setData(gbnbpbean);
			gbnbpcall.callService();
			Logger.comment("gbnbpcall send:" + gbnbpcall.getEntityString());
			Logger.comment("gbnbpcall response:" + gbnbpcall.getReturnData());

			HashMap m = new HashMap();
			m.put("userId", UserId);
			m.put("tradingStatus", list);
			// m.put("startDate", date1);
			// m.put("endDate", date2);
			m.put("productId", ProductId);
			List<Map> data = new TradingQueryDefaultWapper().selectBuyedNumByProductid(m);
			Logger.verifyEquals(ECode.SUCCESS, gbnbpcall.getCode(), "验证Code");
			Logger.verifyEquals((data.get(0).get("ProductCount")), gbnbpcall.getProductCount(),
					"验证数据库查询与接口返回的数据是否一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyedNumByProduct_003() {
		Logger.start(true,"获取已购买商品数量成功查询全部订单状态");
		try {
			// int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int UserId = 444465;
			String date1 = YMTDateUtil.getBeforeOrNextDay(-365);
			String date2 = YMTDateUtil.getBeforeOrNextDay(1);
			// Logger.comment("查询开始时间:"+date1);
			// gbnbpbean.setBeginTime(date1);
			Logger.comment("买家Id:" + UserId);
			gbnbpbean.setBuyerId(UserId);
			// Logger.comment("查询结束时间:"+date2);
			// gbnbpbean.setEndTime(date2);
			// 订单状态
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(4);
			Logger.comment("订单状态:" + list);
			gbnbpbean.setOrderStatusList(list);
			// 产品ID
			String ProductId = "F7952688-87BE-4327-A547-EA19281F1E83";// sit数据
			Logger.comment("产品ID:" + ProductId);
			gbnbpbean.setProductId(ProductId);
			// 发送请求
			gbnbpcall.setData(gbnbpbean);
			gbnbpcall.callService();
			Logger.comment("gbnbpcall send:" + gbnbpcall.getEntityString());
			Logger.comment("gbnbpcall response:" + gbnbpcall.getReturnData());

			HashMap m = new HashMap();
			m.put("userId", UserId);
			m.put("tradingStatus", list);
			// m.put("startDate", date1);
			// m.put("endDate", date2);
			m.put("productId", ProductId);
			List<Map> data = new TradingQueryDefaultWapper().selectBuyedNumByProductid(m);
			Logger.verifyEquals(ECode.SUCCESS, gbnbpcall.getCode(), "验证Code");
			Logger.verifyEquals((data.get(0).get("ProductCount")), gbnbpcall.getProductCount(),
					"验证数据库查询与接口返回的数据是否一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBuyedNumByProduct_004() {
		Logger.start(true,"userid为空,查询失败");
		try {
			int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			String date1 = YMTDateUtil.getBeforeOrNextDay(-365);
			String date2 = YMTDateUtil.getBeforeOrNextDay(1);
			Logger.comment("查询开始时间:" + date1);
			gbnbpbean.setBeginTime(date1);
			Logger.comment("买家Id:" + UserId);
			// gbnbpbean.setBuyerId(UserId);
			Logger.comment("查询结束时间:" + date2);
			gbnbpbean.setEndTime(date2);
			// 订单状态
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(4);
			Logger.comment("订单状态:" + list);
			gbnbpbean.setOrderStatusList(list);
			// 产品ID
			// String ProductId =
			// "7aa433ed-bf8a-4e7e-aee5-1f0d4eba45cb";//alpha数据
			String ProductId = "aa5ddccf-d911-4440-a4c7-d5d2259e4093";// sit数据
			Logger.comment("产品ID:" + ProductId);
			gbnbpbean.setProductId(ProductId);
			// 发送请求
			gbnbpcall.setData(gbnbpbean);
			gbnbpcall.callService();
			Logger.comment("gbnbpcall send:" + gbnbpcall.getEntityString());
			Logger.comment("gbnbpcall response:" + gbnbpcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED400, gbnbpcall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG2, gbnbpcall.getMsg(),
			// "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBuyedNumByProduct_005() {
		Logger.start(true,"userid不存在,查询失败");
		try {
			int UserId = 288;
			String date1 = YMTDateUtil.getBeforeOrNextDay(-365);
			String date2 = YMTDateUtil.getBeforeOrNextDay(1);
			Logger.comment("查询开始时间:" + date1);
			gbnbpbean.setBeginTime(date1);
			Logger.comment("买家Id:" + UserId);
			gbnbpbean.setBuyerId(UserId);
			Logger.comment("查询结束时间:" + date2);
			gbnbpbean.setEndTime(date2);
			// 订单状态
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(4);
			Logger.comment("订单状态:" + list);
			gbnbpbean.setOrderStatusList(list);
			// 产品ID
			// String ProductId =
			// "7aa433ed-bf8a-4e7e-aee5-1f0d4eba45cb";//alpha数据
			String ProductId = "aa5ddccf-d911-4440-a4c7-d5d2259e4093";// sit数据
			Logger.comment("产品ID:" + ProductId);
			gbnbpbean.setProductId(ProductId);
			// 发送请求
			gbnbpcall.setData(gbnbpbean);
			gbnbpcall.callService();
			Logger.comment("gbnbpcall send:" + gbnbpcall.getEntityString());
			Logger.comment("gbnbpcall response:" + gbnbpcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gbnbpcall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG2, gbnbpcall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(0, gbnbpcall.getProductCount(), "验证购买数量");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}